package work.managercenter;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import utils.MyUtils;

public class PostLead {
	public static void main(String[] args) {
		try {
			postLead();
		} catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}


	private static void postLead() throws Exception {
		String urlAddress =  "http://localhost:8080/services/directlead/v1/postNewLead";

		String firstName = ">_<$%&";
		String lastName = "$%&/";
		String emailAddress = "av@ib.com";
		String phoneNum = "9879879871";
		String moveInDate = "09/25/2020";
		String comments = "asdasdasdhttp://screencast.com/t/cXikpGibvhW";
		String complexId = "2131";
		String contactDetails = "email";
		String ipAddress = "127.0.0.1";
		String rentMin = "-1";
		String rentMax = "-1";
		String sendConfirmation = "false";

		System.out.println(urlAddress);


		//		String params = "firstname=" + firstName +
		//				"&lastname=" + lastName +
		//				"&emailAddress=" + emailAddress +
		//				"&complexId=" + complexId +
		//				"&moveInDate=" + moveInDate +
		//				"&comments=" + comments +
		//				"&rentMin=" + rentMin +
		//				"&rentMax=" + rentMax +
		//				"&phone=" + phoneNum +
		//				"&contactPreference=" + contactDetails +
		//				"&sendConfirmation=" + sendConfirmation +
		//				"&ipAddress=" + ipAddress +
		//				"&format=json";
		String params = "firstname=" + java.net.URLEncoder.encode(firstName,"UTF-8") +
				"&lastname=" + java.net.URLEncoder.encode(lastName,"UTF-8") +
				"&emailAddress=" + java.net.URLEncoder.encode(emailAddress,"UTF-8") +
				"&complexId=" + java.net.URLEncoder.encode(complexId,"UTF-8") +
				"&moveInDate=" + java.net.URLEncoder.encode(moveInDate,"UTF-8") +
				"&comments=" + java.net.URLEncoder.encode(comments,"UTF-8") +
				"&rentMin=" + java.net.URLEncoder.encode(rentMin,"UTF-8") +
				"&rentMax=" + java.net.URLEncoder.encode(rentMax,"UTF-8") +
				"&phone=" + java.net.URLEncoder.encode(phoneNum,"UTF-8") +
				"&contactPreference=" + java.net.URLEncoder.encode(contactDetails,"UTF-8") +
				"&sendConfirmation=" + java.net.URLEncoder.encode(sendConfirmation,"UTF-8") +
				"&ipAddress=" + java.net.URLEncoder.encode(ipAddress,"UTF-8") +
				"&format=json";
		System.out.println(params);
		byte[] bytes = params.getBytes("UTF-8");
		URL url = new URL(urlAddress);
		HttpURLConnection conn1 = (HttpURLConnection) url.openConnection();
		conn1.setRequestMethod("POST");
		conn1.setRequestProperty("Accept", "application/json");
		conn1.setRequestProperty("Content-Length", String.valueOf(bytes.length));
		conn1.setDoOutput(true);
		OutputStream outputStream = conn1.getOutputStream();
		outputStream.write(bytes);
		BufferedReader br = new BufferedReader(new InputStreamReader((conn1.getInputStream())));

		String output;
		StringBuilder json = new StringBuilder();

		while ((output = br.readLine()) != null) {
			json.append(output).append(MyUtils.EOL);
		}

		conn1.disconnect();

		System.out.println("json: " + json);
	}
}
